package cn.gov.msa.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.gov.msa.model.UserDetail;
import cn.gov.msa.service.StatisticsService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@RestController
@RequestMapping(value = "/statistics", produces="text/plain;charset=UTF-8")
public class StatisticsController {
	
	private ObjectMapper mapper = new ObjectMapper(); 	
	@Autowired
	StatisticsService statisticsService;
	
	@RequestMapping(value = "/applyStatistics")
	public String applyStatistics(@RequestBody JSONObject params,HttpSession session)throws Exception{
		UserDetail userDetail = (UserDetail) session.getAttribute("userDetail");
		
		if(!params.has("orgCode"))
			params.put("orgCode", userDetail.getOrgcode());
		Map map = mapper.readValue(params.toString(), Map.class);
		statisticsService.applyStatistics(map);
		List<Map> lists  =  (List<Map>) map.get("result");
	     JSONArray ja = JSONArray.fromObject(lists);
		 JSONObject jsonResult = new JSONObject();
		 jsonResult.put("rows", ja);
		 return  jsonResult.toString();
	}
	
	@RequestMapping(value = "/auditorStatistics")
	public String auditorStatistics(@RequestBody JSONObject params,HttpSession session)throws Exception{
		UserDetail userDetail = (UserDetail) session.getAttribute("userDetail");
		
		if(!params.has("orgCode"))
			params.put("orgCode", userDetail.getOrgcode());
		Map map = mapper.readValue(params.toString(), Map.class);
		statisticsService.applyStatistics(map);
		List<Map> lists  =  (List<Map>) map.get("result");
	     JSONArray ja = JSONArray.fromObject(lists);
		 JSONObject jsonResult = new JSONObject();
		 jsonResult.put("rows", ja);
		 return  jsonResult.toString();
	}
	@RequestMapping(value = "/complaintStatistics")
	public String complaintStatistics(@RequestBody JSONObject params,HttpSession session)throws Exception{
		UserDetail userDetail = (UserDetail) session.getAttribute("userDetail");
		
		if(!params.has("orgCode"))
			params.put("orgCode", userDetail.getOrgcode());
		Map map = mapper.readValue(params.toString(), Map.class);
		statisticsService.complaintStatistics(map);
		List<Map> lists  =  (List<Map>) map.get("result");
	     JSONArray ja = JSONArray.fromObject(lists);
		 JSONObject jsonResult = new JSONObject();
		 jsonResult.put("total", ja.size());
		 jsonResult.put("rows", ja);
		 return  jsonResult.toString();
	}
	@RequestMapping(value = "/consultationStatistics")
	public String consultationStatistics(@RequestBody JSONObject params,HttpSession session)throws Exception{
		UserDetail userDetail = (UserDetail) session.getAttribute("userDetail");
		
		if(!params.has("orgCode"))
			params.put("orgCode", userDetail.getOrgcode());
		Map map = mapper.readValue(params.toString(), Map.class);
		statisticsService.consultationStatistics(map);
		List<Map> lists  =  (List<Map>) map.get("result");
	     JSONArray ja = JSONArray.fromObject(lists);
		 JSONObject jsonResult = new JSONObject();
		 jsonResult.put("total", ja.size());
		 jsonResult.put("rows", ja);
		 System.err.println("查询结果："+jsonResult.toString());
		 return  jsonResult.toString();
	}
	
	@RequestMapping(value = "/surveyStatistics")
	public String surveyStatistics(@RequestBody JSONObject params,HttpSession session)throws Exception{
		Map map = mapper.readValue(params.toString(), Map.class);
		List<Map> lists = statisticsService.surveyStatistics(map);
		JSONArray ja = JSONArray.fromObject(lists);
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("total", ja.size());
		jsonResult.put("rows", ja);
		return  jsonResult.toString();
	}
}
